<template>
    <div class="components_edit_modal">
        <el-dialog width="500px" :title="title" :visible.sync="state" :show-close="true" :close-on-click-modal="false">
            <el-form ref="formInfo" :model="formInfo" :rules="formRules" label-width="95px" size="mini">
                <el-form-item label="奖品名称：" prop="name">
                    <el-input v-model.trim="formInfo.name" placeholder="请输入奖品名称"></el-input>
                </el-form-item>
                <el-form-item label="奖品类型：" prop="awardType">
                    <el-radio v-model="formInfo.awardType" label="1">现金</el-radio>
                    <el-radio v-model="formInfo.awardType" label="0">金豆</el-radio>
                </el-form-item>
                <el-form-item label="中奖概率：" prop="chance">
                    <el-input-number v-model="formInfo.chance" :min="1" :max="100" label="请输入中奖概率"></el-input-number> 
                    <!-- <el-input v-model.trim.number="formInfo.chance" placeholder="请输入中奖概率"></el-input> -->
                </el-form-item>
                <el-form-item label="奖品金额：" prop="amount">
                    <el-input-number v-model="formInfo.amount" :min="0.01" :max="99999999" label="请输入奖品金额"></el-input-number>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;元
                    <!-- <el-input v-model.trim.number="formInfo.amount" placeholder="请输入奖品金额"></el-input> -->
                </el-form-item>
                <el-form-item label="奖品描述：" prop="description">
                    <el-input type="textarea" v-model.trim="formInfo.description" placeholder="请输入奖品描述"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" size="small" @click="addInfo('formInfo')">确 定</el-button>
                <el-button @click="state = false" size="small">关 闭</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        components: {},
        data() {
            return {
                state: false,
                title: "",
                type: "",
                imageUrl: "",
                attachment: [], //上传附件
                value: "",
                formInfo: {
                    name: null,
                    chance: null,
                    amount: null,
                    awardType: "0",
                    description: null
                },
                formRules: {
                    name: [{
                        required: true,
                        message: "请输入奖品名称",
                        trigger: "blur"
                    }],
                    awardType: [{
                        required: true,
                        message: "请输入字典名称",
                        trigger: "blur"
                    }],
                    chance: [{
                        required: true,
                        message: "请输入中奖概率",
                        trigger: "blur"
                    }],
                    amount: [{
                        required: true,
                        message: "请输入奖品金额",
                        trigger: "blur"
                    }],
                    description: [{
                        required: true,
                        message: "请输入奖品描述",
                        trigger: "blur"
                    }]
                }
            };
        },
        created() {},
        mounted() {},
        methods: {
            showDialog(val) {
                console.log(val);
                if (this.$refs["formInfo"] !== undefined) {
                    this.$refs["formInfo"].resetFields();
                }
                if (val) {
                    for (var key in val) {
                        this.formInfo[key] = val[key];
                    }
                    this.formInfo.awardType = this.formInfo.awardType + '';
                    this.title = "编辑奖品";
                    this.type = "edit";
                } else {
                    this.title = "添加奖品";
                    this.type = "add";
                }
                this.state = true;
            },
            addInfo() {
                let vm = this;
                vm.$refs.formInfo.validate(valid => {
                    if (valid) {
                        let submission = ""; //提交方式
                        //选择提交方式
                        if (vm.type == "edit") {
                            submission = "put";
                        } else {
                            submission = "post";
                        }
                        // 提交
                        this.axios({
                            method: submission,
                            url: api.activityManagement.prizesList,
                            data: vm.formInfo
                        }).then(response => {
                            if (response.data.code == 200) {
                                vm.state = false;
                                vm.$parent.$refs.tableList.$emit("vuetable:refresh");
                                vm.$message.success(response.data.message);
                            } else {
                                vm.$message.error(response.data.message);
                            }
                        });
                    } else {
                        return false;
                    }
                });
            }
        }
    };
</script>
<style scoped>

</style>